//
//  CrossTroopForm.cpp
//  fingeradvanture
//
//  Created by shuaibincheng on 16/5/9.
//
//

#include "CrossTroopForm.hpp"

void CrossTroopForm::group()
{
    auto horizontalSubGroup = getHorizontalSubGroup();
    auto verticalSubGroup = getVerticalSubGroup();
    
    groupInternal(horizontalSubGroup);
    
    auto mid = horizontalSubGroup.at(horizontalSubGroup.size() / 2);
    float x = 0.0f, y = 0.0f;
    mid->getPosition(&x, &y);
    
    troopSize.height = 0.0f;
    int i = 0;
    for(auto it = verticalSubGroup.begin(); it != verticalSubGroup.end(); it++,i += 2)
    {
        (*it)->setRotation(i * 90.0f);
        (*it)->setPosition(x, y);
        troopSize.height += std::abs((*it)->getAnchorPoint().y * (*it)->getContentSize().height);
    }
}

void CrossTroopForm::train()
{
    getAction()->train(*members);
}

float CrossTroopForm::marginRight()
{
    auto last = members->at(members->size() - 3);
    auto position = last->getPosition();
    
    auto screenWidth = (Director::getInstance()->getVisibleSize()).width;
    
    auto rightX = 0.0f;
    if(troopVisibleSize.width > screenWidth)
    {
        rightX = screenWidth + (troopVisibleSize.width - screenWidth) / 2;
    }
    else
    {
        rightX = screenWidth;
    }
    auto anchorPoint = last->getAnchorPoint();
    return -(position.x + (1 - anchorPoint.x) * last->getContentSize().width) + rightX;
}


std::vector<TroopStyleType> CrossTroopForm::allowedTroopStyles()
{
    std::vector<TroopStyleType> types;
    types.push_back(TYPE_CROSSTWOSTYLESSTYLE);
    return types;
}

std::vector<TroopActionStyle> CrossTroopForm::allowedTroopActionStyle()
{
    std::vector<TroopActionStyle> types;
    types.push_back(TYPE_CROSSFORM);
    return types;
}

Vector<Node *> CrossTroopForm::getVerticalSubGroup()
{
    Vector<Node *> verticalSubGroup;
    for(auto i = members->size() - 2; i < members->size(); i++)
    {
        verticalSubGroup.pushBack(members->at(i));
    }
    return verticalSubGroup;
}

Vector<Node *> CrossTroopForm::getHorizontalSubGroup()
{
    Vector<Node *> horizontalSubGroup;
    for(auto i = 0; i < members->size() - 2; i++)
    {
        horizontalSubGroup.pushBack(members->at(i));
    }
    return horizontalSubGroup;
}